<?php
header('Content-Type: image/gif');
readfile('counter.gif');

require_once('/var/astolfo/config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('backend', 'dev', true);
$context = sfContext::createInstance($configuration);

if($_REQUEST['asset_id'] > 0 || $_REQUEST['section_id'] > 0){
  $view = new View();
  $view->asset = $_REQUEST['asset_id'];
  $view->section = $_REQUEST['section_id'];
  $view->ip = $_REQUEST['ip'];
  $view->save();

  if($_REQUEST['asset_id'] > 0){
    $asset = Doctrine_Core::getTable('Asset')->findOneById($_REQUEST['asset_id']);
    if($asset){
      // asset view counter
      $q = Doctrine::getTable('asset')->createQuery();
      $q->update()
        ->set('views', '?', $asset->views+1)
        ->set('updated_at', '?', $asset->updated_at)
        ->where('id = ?', $asset->id)
        ->execute();
    }
  }
}
die();
